Feb 27th 2017 by Margaret

At the beginning, we had an overview about what postfix, infix and prefix are by a review about the construction of a tree.
We practiced on drawing a tree in accordance to the simple code to visualize the differences among them:
A. prefix: node + left + right
B. infix: left + node +right
C. postfix: left + right + node
The code of the three different types of tree if posted below:
import turtle
 
def tree(branchlen,t):
    if branchlen>20:
        t.forward(branchlen)
        t.right(20)
        # A
        # t.circle(1, None, None)
        tree(branchlen-15,t)
        # B
        # t.circle(1, None, None)
        t.left(40)
        tree(branchlen-15,t)
        # C
        # t.circle(1, None, None)
        t.right(20)
        t.backward(branchlen)
 
def main():
    t=turtle.Turtle()
    myWin = turtle.Screen()
    t.left(90)
    t.up()
    t.backward(100)
    t.down()
    t.color("green")
    t.speed(10)
    tree(75,t)
    myWin.exitonclick()
 
main()

Apart from working hard to figure out the real sequence of output, we can also have an effortless method by drawing flags beside each node. For example, with infix, a little flag can be drawn on the left side of each node and the sequence is shown as we draw a line to link them up smoothly. Also, you can try to flatten the tree to get a result of infix.
FullSizeRender 18.jpg

This method can also be utilized when we are expecting different outputs changed by different ways to traverse a tree. For example, when we have a tree as:
FullSizeRender 17.jpg

class Tree:
    def __init__(self, cargo, left=None, right=None):
        self.cargo = cargo
        self.left = left
        self.right = right
 
    def __str__(self):
        return str(self.cargo)
 
tree = Tree("i",Tree("b"),Tree("c",Tree("s")))
 
def print_tree1(tree):
    if tree is None: return
    print(tree.cargo, end=" ")
    print_tree1(tree.left)
    print_tree1(tree.right)
 
def print_tree2(tree):
    if tree is None: return
    print_tree2(tree.left)
    print(tree.cargo, end=" ")
    print_tree2(tree.right)
 
def print_tree3(tree):
    if tree is None: return
    print_tree3(tree.left)
    print_tree3(tree.right)
    print(tree.cargo, end=" ")
 
print_tree1(tree)
print_tree2(tree)
print_tree3(tree)
 
  • With prefix Tree1, we get: i b c s
  • With infix Tree2, we get: b i s c
  • With postfix Tree3, we get: b s c i

As we proceed on understanding prefix, infix and postfix well, we start to learn about the RPN calculator.
First of all, we made a comparison between an ordinary calculator and a RPN calculator. It turned out that a RPN calculator need the user to press less buttons and therefore is more convenient and time-efficient to use. Meanwhile, as there is no ambiguous interpretation about the existence of parenthesis and the sequence of calculation in RPN, it is widely used in lots of circumstances where precise calculations are required.

With an ordinary calculator using infix and a RPN calculator using postfix, samples ra given here:
Infix
2 * 3 + 5
2 * (3 + 5)
Postfix
2 3 * 5 +
2 3 5 + *

What actually happens here is that a stack is used in RPN: when a number is appended to the stack, it will accumulate in there, but when a notation such as “+” or “*” is appended, the last two numbers will be “popped” and be calculated. With this logic, a sample code is provided here:
 import math
 
def postfix_evaluation(s):
    s = s.split()
    n = len(s)
    stack = []
 
    for i in range(n):
        if s[i].isdigit():
            stack.append(int(s[i]))
        elif s[i]=="+":
            a = stack.pop()
            b = stack.pop()
            stack.append(int(a)+int(b))
        elif s[i] =="*":
            a = stack.pop()
            b = stack.pop()
            stack.append(int(a)*int(b))
        elif s[i] =="-":
            a = stack.pop()
            b = stack.pop()
            stack.append(int(a) - int(b))
        elif s[i] =="/":
            a = stack.pop()
            b = stack.pop()
            stack.append(int(a)/int(b))
        elif s[i] =="%":
            a = stack.pop()
            b = stack.pop()
            stack.append(int(a) % int(b)
 
    return stack.pop()
 
s = "10 2 8 * + 3 -"
val = postfix_evaluation(s)
print(val)
 
With the help of it, we can create a calculator with more functions, such as:
  • various methods including “-” “/ ” “%” “sin” “cos” etc.
  • change the postfix into a normal look by printing out the infix version of it